package com.qcby.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.qcby.Constants;
import com.qcby.dao.UserMapper;
import com.qcby.model.TicketOrder;
import com.qcby.model.User;
import com.qcby.model.dto.OrderQueryDto;
import com.qcby.model.dto.OrderVo;
import com.qcby.model.dto.TicketOrderVo;
import com.qcby.model.resp.RespEntity;
import com.qcby.query.TicketOrderQuery;
import com.qcby.service.TicketOrderService;
import com.qcby.util.Page;

@RestController
@RequestMapping("/order")
public class TicketOrderController {
    @Autowired
    private TicketOrderService orderService;
    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/list")
    public RespEntity<OrderVo> searchOrder(TicketOrderQuery tq){
        if(tq!=null){
            if (tq.getPageNo() == null){
                tq.setPageNo(1);
                Page<TicketOrder> page=orderService.selectObjectByCondition(tq,"0");
                return RespEntity.successCustomize(Constants.FLAG_200,"查询成功",new OrderVo(page,tq));
            }

        }
        return RespEntity.errorCustomize(Constants.FLAG_400,"查询失败",null);
    }

    @PostMapping("/selectCondition")
    public RespEntity<Page<TicketOrderVo>> selectCondition(@RequestBody OrderQueryDto queryDto) {
        Integer uid = queryDto.getUid();
        if (uid == null) {
            return RespEntity.errorNoData(Constants.FLAG_400, "用户id不能为空！");
        }
        User user = userMapper.selectByPrimaryKey(uid);
        // 超级管理员查询订单不需要被uid筛选
        if (user.getURole() == "0") {
            queryDto.setUid(null);
        }
        if (queryDto.getPageNo() == null) {
            queryDto.setPageNo(1);
        }
        return RespEntity.successCustomize(Constants.FLAG_200, "查询订单成功！", orderService.selectCondition(queryDto));
    }
}
